/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.notification.impl;

import com.ceair.flight.refund.service.notification.ISimpleNotification;
import com.ceair.flight.refund.service.notification.MessageEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Colin_You
 */
abstract class BaseNotification implements ISimpleNotification {

    private static final Logger LOG = LoggerFactory.getLogger(BaseNotification.class);
    @Autowired
//    private IEnvUtil envUtil;
    @Resource(name = "emailNotification")
    private ISimpleNotification emailNotification;
    @Resource(name = "smsNotification")
    private ISimpleNotification smsNotification;

    private static String smsContentPrefix;
    private static String emailSubjectSuffix;

//    @PostConstruct
//    void initial() {
//        smsContentPrefix = "Message from " + envUtil.getEnv() + " env: ";
//        emailSubjectSuffix = " on " + envUtil.getEnv() + " env";
//    }

    void notifyWithEmail(Map args) {
        Map<String, Object> emailArgs = new HashMap<>(args);
        String subject = getSubject() + emailSubjectSuffix;
        LOG.info("Email subject: {}", subject);
        emailArgs.put(MessageEnum.SUBJECT.getVarName(), subject);

        Object content = getEmailContent(args);
        if (content == null) {
            LOG.error("Empty email content, stop sending email");
            return;
        }
        LOG.info("Email content: {}", content);
        emailArgs.put(MessageEnum.CONTENT.getVarName(), content);
        emailArgs.put(MessageEnum.RECEIVER.getVarName(), getEmailReceiver());
        try {
            emailNotification.notify(emailArgs);
        } catch (Exception e) {
            LOG.error("Retrive email id error, stopping sending email, email content:{}", content, e);
        }
    }

    void notifyWithSms(Map args) {
        Map<String, Object> emailArgs = new HashMap<>(args);

        Object content = getSmsContent(args);
        if (content == null) {
            LOG.error("Empty Sms content, stop sending sms");
            return;
        }
        content = smsContentPrefix + content;
        LOG.info("Sms content: {}", content);
        emailArgs.put(MessageEnum.CONTENT.getVarName(), content);

        emailArgs.put(MessageEnum.MOBILES.getVarName(), getSmsReceiver());
        smsNotification.notify(emailArgs);
    }

    Object getEmailContent(final Map args) {
        Object content = args.get(MessageEnum.CONTENT.getVarName());
        if (content instanceof Exception) {
            Exception e = (Exception) content;
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsString = sw.toString();
            LOG.debug("Email with exception content \n{}", exceptionAsString);
            return exceptionAsString;
        }
        return content;
    }

    Object getSmsContent(final Map args) {
        Object content = args.get(MessageEnum.CONTENT.getVarName());
        if (content instanceof Exception) {
            return ((Exception) content).getMessage();
        }
        return content;
    }

    abstract String getSubject();

    abstract String getEmailReceiver();

    abstract String getSmsReceiver();
}
